package codingforgreat.lchot150;

import java.util.LinkedList;

public class Lc239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        LinkedList<Integer> qmax = new LinkedList<>();
        int[] res = new int[nums.length - k + 1 ];
        int index = 0;
        int L = 0;
        for(int R = 0;R < nums.length;R++){
            while(!qmax.isEmpty() && nums[qmax.peekFirst()] <= nums[R]){
                qmax.pollFirst();
            }
            qmax.addFirst(R);
            if(qmax.peekLast() <= R - k){
                qmax.pollLast();
            }

            if(R >= k - 1){
                res[index++] = nums[qmax.peekLast()];
            }
        }
        return res;
    }
}
